package exam;

import java.util.HashMap;
import java.util.Scanner;

public class test1 {

    public static void main(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int len=scanner.nextInt();
        String str1=scanner.next();
        String str2=scanner.next();
        String []strs = new String[len];
        if(str1==null||str2==null){
            System.out.println(-1);
            return;
        }
        for (int i = 0; i <len ; i++) {
            strs[i]= scanner.next();
        }
        int min=Integer.MAX_VALUE;
        HashMap<String,Integer>map=new HashMap<>();
        for (int i = 0; i <len ; i++) {
         if(strs[i].equals(str1)){
                 if(map.containsKey(str2)){
                     min=Math.min(min,i-map.get(str2));
                 }
             map.put(str1,i);
         }

            if(strs[i].equals(str2)){
                    if(map.containsKey(str1)){
                        min=Math.min(min,i-map.get(str1));
                    }
                map.put(str2,i);
                }
        }
        if(!(map.containsKey(str1)&&map.containsKey(str2))){
            System.out.println(-1);
            return;
        }
        System.out.println(min);
    }


    public static void main2(String[] args) {
        Scanner scanner=new Scanner(System.in);
        int len= scanner.nextInt();
        int []arr=new int[len+1];
        for (int i = 0; i <len ; i++) {
            arr[i]= scanner.nextInt();
        }
        if (len<=2){
            System.out.println(0);
            return;
        }
        for (int i = 2; i <=len; i++) {
            arr[i]=Math.min(arr[i-1],arr[i-2])+arr[i];
        }
        System.out.println(arr[len]);
    }



    public static void main1(String[] args) {
        Scanner scanner=new Scanner(System.in);
        double a= scanner.nextDouble();
        String c=scanner.nextLine();
        char b=c.charAt(1);
        if (a<=1){
            if (b=='y'){
                System.out.println(25);
            }else {
                System.out.println(20);
            }
        }else {
            if(a-(int)a>0){
                if(b=='y'){
                    System.out.println(25+(int)a);
                }else {
                    System.out.println(20+(int)a);
                }
            }else {
                if(b=='y'){
                    System.out.println(25+(int)a-1);
                }else {
                    System.out.println(20+(int)a-1);
                }
            }
        }
    }
}
